// Copyright 2023 The titan Authors. All rights reserved.

// Project: titan
// CodeName: 泰坦
// IDE: GoLand
// Author: fucaihe@outlook.com
// File: main.go
// Date: 2023/12/18 14:44:20
// Description: 数据去重系统 - 客户端

package main

import (
	"context"
	"log"
	"time"

	"github.com/smallnest/rpcx/client"

	"titan/internal/api"
)

func main() {
	d, err := client.NewPeer2PeerDiscovery("tcp@127.0.0.1:9000", "")
	if err != nil {
		log.Fatalln(err)
	}

	option := client.DefaultOption
	option.Heartbeat = true
	option.HeartbeatInterval = 10 * time.Second

	xClient := client.NewXClient("Arith", client.Failtry, client.RandomSelect, d, option)
	defer func() { _ = xClient.Close() }()

	for {
		now := time.Now()

		args := &api.ArithArgs{
			A: 10,
			B: 2,
		}

		reply := &api.ArithReply{}
		if err = xClient.Call(context.Background(), "Add", args, reply); err != nil {
			log.Fatalln("failed to call:", err)
		}

		log.Println("reply ->", args.A, args.B, reply.C)

		log.Println("---------- cost ----------", time.Since(now).Milliseconds())

		time.Sleep(5 * time.Second)
	}
}
